package com.bw.sms.service.imp;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.core.creditStatus.DeleteStatus;
import com.bw.core.vo.NoteMsgVo;
import com.bw.sms.mapper.TbSmsLogMapper;
import com.bw.sms.pojo.TbSmsLog;
import com.bw.sms.service.TbSmsLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;

/**
 * <p>
 * 短信日志记录表 服务实现类
 * </p>
 *
 * @author lv
 * @since 2021-03-23
 */
@Service
public class TbSmsLogServiceImpl extends ServiceImpl<TbSmsLogMapper, TbSmsLog> implements TbSmsLogService {
    @Autowired
    private TbSmsLogMapper mapper;

    @Override
    public void noteMsg(String msg) {
        JSONObject jsonObject = JSONObject.parseObject(msg);
        NoteMsgVo noteMsgVo = JSONObject.toJavaObject(jsonObject, NoteMsgVo.class);

        TbSmsLog smsLog = new TbSmsLog();
        smsLog.setSmsPhone(noteMsgVo.getPhone());
        smsLog.setSmsTime(new Date());//发送时间
        smsLog.setSmsMsg(noteMsgVo.getNoteMsg());
        smsLog.setSmsType(noteMsgVo.getSmsType());
        smsLog.setIsDelete(DeleteStatus.NOT_DELETE);
        smsLog.setCreateTime(new Date());
        smsLog.setUpdateTime(new Date());

        mapper.insert(smsLog);

        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream("credit-sms/src/main/resources/bootstrap.yml"));
            String merId = (String) prop.get("merId");
            String appSercret = (String) prop.get("appSercret");

            HashMap<String, Object> map = new HashMap<>();

            map.put("merId", merId);
            map.put("sign",appSercret);
            map.put("mobile",noteMsgVo.getPhone());
            map.put("content",noteMsgVo.getNoteMsg());
            String post = HttpUtil.post("http://localhost:8080/api/common/sendSms", map);
            System.out.println("短信发送成功之后回执："+post);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
